<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="openSheet"
    width="800px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('samplesItem')" prop="item_no" :error="error.item_no">
            <el-input
              v-model="form.item_no"
              maxlength="15"
              :disabled="!createFlag"
              show-word-limit
              :placeholder="$t('inputSamplesItem')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesMenuCode')" prop="menu" :error="error.menu">
            <el-select
              v-model="form.menu"
              style="width: 100%"
            >
              <el-option v-for="item in menuOptions" :key="item.id" :label="item.code" :value="item.id">
                <span style="float: left">{{ item.code }}</span>
                <span style="float: right; font-size: 13px; color: var(--el-text-color-secondary)">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesWarehouse')" prop="warehouse" :error="error.warehouse">
            <el-input
              v-model="form.warehouse"
              maxlength="15"
              show-word-limit
              :placeholder="$t('inputSamplesWarehouse')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesLocation')" prop="location" :error="error.location">
            <el-input
              v-model="form.location"
              maxlength="15"
              show-word-limit
              :placeholder="$t('inputSamplesLocation')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesValidDate')" prop="valid_date" :error="error.valid_date">
            <el-date-picker
              v-model="form.valid_date"
              :placeholder="$t('inputSamplesValidDate')"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesInvalidDate')" prop="invalid_date" :error="error.invalid_date">
            <el-date-picker
              v-model="form.invalid_date"
              :placeholder="$t('inputSamplesInvalidDate')"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesQuantity')" prop="quantity" :error="error.quantity">
            <el-input
              v-model="form.quantity"
              type="number"
              :placeholder="$t('inputSamplesQuantity')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('samplesNote')" prop="note" :error="error.note">
            <el-input
              v-model="form.note"
              type="textarea"
              :rows="1"
              autosize
              :placeholder="$t('inputSamplesNote')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" @click="submitForm">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" @click="submitForm">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import {
  createSamplesApi,
  updateSamplesApi
} from '../api/samples.api';
import { getDropDownMenuListApi } from '@/api/common/common'
export default {
  directives: { elDragDialog },
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      sampleId: undefined,
      menuOptions: [],
      form: {
        item_no: undefined,
        warehouse: undefined,
        location: undefined,
        menu: undefined,
        valid_date: undefined,
        invalid_date: undefined,
        note: undefined,
        quantity: undefined
      },
      error: {
        item_no: undefined,
        warehouse: undefined,
        location: undefined,
        menu: undefined,
        valid_date: undefined,
        invalid_date: undefined,
        note: undefined,
        quantity: undefined
      }
    };
  },
  computed: {
    rules() {
      return {
        item_no: [
          {
            required: true,
            message: this.$t('samplesItemRequire'),
            trigger: 'blur'
          }
        ],
        menu: [
          {
            required: true,
            message: this.$t('samplesMenuCodeRequire'),
            trigger: 'blur'
          }
        ],
        valid_date: [
          {
            required: true,
            message: this.$t('samplesValidDateRequire'),
            trigger: 'blur'
          }
        ],
        invalid_date: [
          {
            required: true,
            message: this.$t('samplesInvalidDateRequire'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async openSheetDialog(data) {
      this.openSheet = !this.openSheet;
      this.getMenuOption();
      this.reset();
      if (data) {
        this.title = this.$t('samplesUpdate');
        this.createFlag = false;
        this.sampleId = data.id;
        this.form = data;
      } else {
        this.title = this.$t('samplesCreate');
        this.createFlag = true;
        this.sampleId = undefined;
      }
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form)
    },
    async getMenuOption() {
      var resp = await getDropDownMenuListApi();
      if (resp.success) {
        this.menuOptions = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    submitForm: function() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.resetJsonObject(this.error)
          var resp;
          if (this.createFlag) {
            resp = await createSamplesApi(this.form);
          } else {
            resp = await updateSamplesApi(this.sampleId, this.form);
          }
          if (resp.success) {
            this.$emit('refreshList');
            this.cancel();
            if (this.createFlag) {
              this.targaMessage(
                this.$t('createSuccess', {
                  title: this.$t('samplesItem'),
                  value: this.form.item_no
                })
              );
            } else {
              this.targaMessage(
                this.$t('updateSuccess', {
                  title: this.$t('samplesItem'),
                  value: this.form.item_no
                })
              );
            }
          } else {
            if (typeof resp.msg === 'object') {
              this.error = resp.msg;
            } else {
              this.targaMessageBox(this, resp.msg);
            }
          }
        }
      });
    }
  }
};
</script>

  <style></style>

